package com.game.modular.game.battle.process.skillEffectHandle.base;

import com.game.modular.game.battle.panel.Battle;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.game.battle.panel.skillEffect.domain.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * 处理
 * 数值|叠层
 */
@Slf4j
@Component
public class EffectHandle4 {
    /**
     * 处理
     * 数值|叠层
     */
    public void tryHandleLayerValue(PvpSkillEffect pvpSkillEffect) {
        SkillEffectStatus status = pvpSkillEffect.getStatus();
        if (status.getStatus() == 0 &&
                status.getIsTrigger() != 1
        ) {
            return;
        }
        noLayerRefresh(pvpSkillEffect);
    }


    /**
     * 无叠层
     * 技能效果
     */
    private static void noLayerRefresh(
            PvpSkillEffect pvpSkillEffect
    ) {
        Integer status = pvpSkillEffect.getStatus().getStatus();
        if (status == 1) {
            newLayer(pvpSkillEffect);
        }if (status == 2) {
            tryWeakLay(pvpSkillEffect);
        }
    }


    private static void newLayer(
            PvpSkillEffect pvpSkillEffect
    ) {
        BaseEffectValueInfo baseEffectValueInfo = pvpSkillEffect.getBaseEffectValueInfo();
        Long battleId = pvpSkillEffect.getBattleId();
        Battle battle = Battle.get(battleId);
        Integer round = battle.getRound();

        SkillEffectLayer skillEffectLayer = pvpSkillEffect.getSkillEffectLayer();
        Double triggerValue = baseEffectValueInfo.getTriggerValue();
        Map<Integer, LayerValue> layerValueMap = skillEffectLayer.getLayerValueMap();
        int targetLayerNumber = layerValueMap.size() + 1;
        LayerValue build = LayerValue.builder()
                .layerNumber(targetLayerNumber)
                .layerRound(round)
                .refreshRound(0)
                .triggerCount(1)
                .thisDecayValue(0.0)
                .totalDecayValue(0.0)
                .lastChangeValue(0.0)
                .thisChangeValue(triggerValue)
                .totalChangeValue(triggerValue)
                .build();
        layerValueMap.put(targetLayerNumber, build);
        skillEffectLayer.setLayerValueMap(layerValueMap);
        skillEffectLayer.obtainThisChangeValue();
        pvpSkillEffect.setSkillEffectLayer(skillEffectLayer);
    }
    private static void tryWeakLay(
            PvpSkillEffect pvpSkillEffect
    ) {
        BaseEffectValueInfo baseEffectValueInfo = pvpSkillEffect.getBaseEffectValueInfo();
        SkillEffectLayer skillEffectLayer = pvpSkillEffect.getSkillEffectLayer();
        Map<Integer, LayerValue> layerValueMap = skillEffectLayer.getLayerValueMap();
        Double decayValue = baseEffectValueInfo.getDecayValue();
        layerValueMap.forEach((key, layValue)->{
            LayerValue layerValue = layerValueMap.get(key);
            layerValue.refreshDecayValue(decayValue);
            layerValueMap.put(key, layerValue);
        });
        skillEffectLayer.obtainThisChangeValue();
        pvpSkillEffect.setSkillEffectLayer(skillEffectLayer);
    }
}
